﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Utilities
{
    /// <summary>
    /// From http://www.simple-talk.com/community/blogs/robertchipperfield/archive/2009/09/25/74986.aspx
    /// </summary>
    public static class SerializationCloner
    {
        public static T Clone<T>(T prototype)
        {
            BinaryFormatter bf = new BinaryFormatter();
            using (MemoryStream ms = new MemoryStream())
            {
                bf.Serialize(ms, prototype);
                ms.Seek(0, SeekOrigin.Begin);
                return (T)bf.Deserialize(ms);
            }
        }
    }
}